//
//  YearPickerViewController.swift
//  miaoyin
//
//  Created by David on 2017/2/23.
//  Copyright © 2017年 David. All rights reserved.
//

import UIKit

class YearPickerViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent
    }
    
    var picker: UIPickerView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "入学年份"
        navigationController?.setNavigationBarHidden(true, animated: false)
        view.backgroundColor = UIColor.white
        
        let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 64))
        navBar.barTintColor = themeColor2
        navBar.barStyle = .black
        navBar.tintColor = UIColor.white
        
        
        let navItem = UINavigationItem(title: "入学年份")
        navItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "navbar_back"), style: .plain, target: self, action: #selector(YearPickerViewController.goBack))
        navItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "arrow_white"), style: .plain, target: self, action: #selector(YearPickerViewController.nextStep))
        
        navBar.pushItem(navItem, animated: true)
        
        
        picker = UIPickerView(frame: CGRect(x: 0, y: 64, width: view.bounds.width, height: 300))
        picker.delegate = self
        picker.dataSource = self
        view.addSubview(picker)
        
        view.addSubview(navBar)
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        let format = DateFormatter()
        format.dateFormat = "yyyy"
        let thisYear = format.string(from: Date())
        let dur =  Int(thisYear)! - 2010
        return dur > 0 ? dur : 0
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return "\(2010 + row) 年"
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func goBack(){
        _ = navigationController?.popViewController(animated: true)
    }
    
    func nextStep(){
        let year = picker.selectedRow(inComponent: 0) + 2010
        print(year)
        if FCUserReg.shared.isRegisterStarted{
            FCUserReg.shared.year = "\(year)"
        }
        navigationController?.pushViewController(UserEditViewController(), animated: true)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
